/*
 * Copyright (c) 2011, Andrew Cole
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 *
 * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 * THE POSSIBILITY OF SUCH DAMAGE.
 */

package net.andycole.xmiparser;

import org.apache.log4j.Logger;

import com.martiansoftware.jsap.JSAP;
import com.martiansoftware.jsap.JSAPException;
import com.martiansoftware.jsap.JSAPResult;
import com.martiansoftware.jsap.stringparsers.EnumeratedStringParser;
import com.martiansoftware.jsap.FlaggedOption;

/**
 *
 * @author andy
 */
public class CommandLineHandler {

   private JSAPResult config;
   private JSAP       jsap;

   private Logger LOG = Logger.getLogger(CommandLineHandler.class);

   private JSAPResult parseCommandLineArguments( final String[] args,
                                                 final JSAP jsap ) throws JSAPException
   {
      return jsap.parse( args );
   }

   public CommandLineHandler() throws JSAPException
   {
      jsap = new JSAP();
      init();
   }

   private void registerParameter(JSAP jsap,
                                  CommandLineOptions options,
                                  boolean isRequired,
                                  String helpString) throws JSAPException
   {
      String optionName = options.toString();
      jsap.registerParameter( new FlaggedOption( optionName ).setStringParser( JSAP.STRING_PARSER )
                                                             .setRequired( isRequired )
                                                             .setShortFlag( optionName.charAt( 0 ) )
                                                             .setLongFlag( optionName ) );
   }

   private void init() throws JSAPException
   {
      registerParameter( jsap,
                         CommandLineOptions.PROJECTDIR,
                         true, "" );

      String optionName = CommandLineOptions.XMIWRITERFORMAT.toString();
      jsap.registerParameter( new FlaggedOption( optionName ).setStringParser( EnumeratedStringParser.getParser("ArgoUML;EMF;netbeans") )
                                                             .setRequired( false )
                                                             .setShortFlag( optionName.charAt( 0 ) )
                                                             .setLongFlag( optionName )
                                                             .setHelp("The application output format: ArgoUml | EMF | netbeans"));
      registerParameter( jsap,
                         CommandLineOptions.OUTPUT,
                         false, "" );
   }
   
   public boolean areCommandLineOptionsCorrect( final String[] args ) throws JSAPException
   {
      config = parseCommandLineArguments( args,
                                          jsap );

      if ( !config.success() )
      {
         // Can also use config.getErrorMessageIterator();
//         System.err.println(
//                     "Usage: java "
//                           + Main.class.getName() + " " + jsap.getUsage() );
//         System.err.println();
//         System.err.println(jsap.getHelp());
         LOG.error("Usage: java "
                      + Main.class.getName() + " " + jsap.getUsage());
         LOG.error("");
         LOG.error(jsap.getHelp());
      }

      return config.success();
   }

   public String getParameterValue( final CommandLineOptions option )
   {
      return config.getString( option.toString() );
   }
}
